/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.qframe.client.quicklet.beans;

import org.qframe.client.render.DomUtils;

/**
 *
 * @author gprasad
 */
public class RenderedSlot extends Slot {

    private RenderedQuicklet parentQuicklet;
    private RenderedQuicklet currentQuicklet;
    private String renderedSlotId;

    public RenderedSlot(String slotId, Quicklet defaultQuicklet, RenderedQuicklet parentQuicklet) {
        super(slotId, defaultQuicklet);
        this.parentQuicklet = parentQuicklet;
        prepareRenderedSlotId();
    }

    public RenderedSlot(Slot slot, RenderedQuicklet parentQuicklet) {
        super(slot.getSlotId(), slot.getDefaultQuicklet());
        this.parentQuicklet = parentQuicklet;
        prepareRenderedSlotId();
    }

    /**
     * Returns the container Quicklet.
     * A Quicklet can contain many Slots.
     *
     * @return Quicklet
     */
    public RenderedQuicklet getParentQuicklet() {
        return parentQuicklet;
    }

    /**
     * Returns the Quicklet that is currently rendered in this slot.
     * returns null if nothing is yet rendered.
     *
     * @return Quicklet
     */
    public RenderedQuicklet getCurrentQuicklet() {
        return currentQuicklet;
    }

    /**
     * Sets the currently rendered quicklet.
     * NOTE: This also changes the quicklet's container slot.
     *
     * @param quicklet
     */
    public void setCurrentQuicklet(RenderedQuicklet quicklet) {
        currentQuicklet = quicklet;
        currentQuicklet.setContainerSlot(this);
    }

    private void prepareRenderedSlotId() {
        if (getParentQuicklet() == null || getParentQuicklet().getContainerSlot() == null) {
            renderedSlotId = DomUtils.TOPMOST_SLOT_PREFIX + DomUtils.SLOT_ID_PARTITION + getSlotId();
        } else {
            renderedSlotId = getParentQuicklet().getContainerSlot().getRenderedSlotId() + DomUtils.SLOT_ID_PARTITION + getSlotId();
        }
    }

    public String getRenderedSlotId() {
        return renderedSlotId;
    }

    @Override
    public String toString() {
        return "RenderedSlot:" + renderedSlotId;
    }
}
